package tree;

import java.util.ArrayList;

import resources.State;

import dataType.Man;
import dataType.Person;
import dataType.Woman;

public class Tree {
	private Node root;

	public Tree() {
		super();
	}
	
	public Tree(Node rootNode) {
		super();
		this.root = rootNode;
	}
	
	public void setRoot(Node root){
		this.root = root;
	}
	
	public void printTree(){
		printTree(root);
	}										//good
	
	private void printTree(Node actual){
		Man man = (Man) actual.getData(); System.out.println(man.getName());
		Woman woman = null;
		if(man.getMarriedWith().getData()!=null){
			woman = (Woman) ((Node) man.getMarriedWith()).getData(); System.out.println(woman.getName());
		}
		if(actual.getChildNodes()!=null)
		for(Node sun : actual.getChildNodes()) {  
			  printTree(sun);  
		} 
		
	}
	
	public Node searchNode(String name){
		return searchNode(root, name);
	}
	
	private Node searchNode(Node actual, String name){
		Man man = (Man) actual.getData(); System.out.println(man.getName());
		if(man.getName().equals(name))
			return actual;
		Woman woman = null;
		if(man.getMarriedWith().getData()!=null){
			woman = (Woman) ((Node) man.getMarriedWith()).getData(); System.out.println(woman.getName());
			if(woman.getName().equals(name))
				return actual;
		}
		if(actual.getChildNodes()!=null)
			for(Node sun : actual.getChildNodes()) {  
				actual = searchNode(sun, name);  
			} 
		return actual;
	}
	
	public ArrayList<Node> getDescendants(String name){
		ArrayList<Node> descendants = new ArrayList<Node>();
		Node actual = searchNode(name);				
		if(actual.getChildNodes()!=null)				//test needed
			for(Node sun : actual.getChildNodes()) {  
				descendants.add(searchNode(sun, name));  
			} 
		return descendants;
	}
	
	public ArrayList<Node> getAncestrals(String name){			//problem
		ArrayList<Node> ancestrals = new ArrayList<Node>();
		Node n = searchNode(name);
		ancestrals  = searchAncestralNode(root, n, ancestrals);
		return ancestrals;
	}
	
	private ArrayList<Node> searchAncestralNode(Node actual, Node n, ArrayList<Node> ancestrals){
		Man man = (Man) actual.getData();
		if(man!=)
			ancestrals.add(actual);
			
		Woman woman = null;
		if(man.getMarriedWith().getData()!=null){
			woman = (Woman) ((Node) man.getMarriedWith()).getData(); ancestrals.add(actual);
			if(!woman.getName().equals(name))
				ancestrals.add(actual);
		}
		
		if(actual.getChildNodes()!=null)
			if(actual.getChildNodes().contains(o))
			for(Node sun : actual.getChildNodes()) {  
				ancestrals.addAll(searchAncestralNode(sun, name, ancestrals));  
			} 
		
	}
	
	public void insert(String parentName, Node nodeToInsert){
		//here we need to insert 
		Node parent = searchNode(parentName);		//ok
		parent.addChild(nodeToInsert);
	}
	
	public void remove(String name){
		Node node = searchNode(name);
		//needs to control if is the man or the woman!!			//probably working great
		Man man = (Man) node.getData();
		Woman woman = (Woman) ((Node) man.getMarriedWith()).getData();
		if(man.getName().equals(name))
			man.setState(State.deceased);
		if(woman.getName().equals(name))
			woman.setState(State.deceased);
			
	}

	/*
	public void search(String nodeName){		//that works?
		Man man = (Man) root.getData();
		if(man.isEquals(nodeName));
			System.out.println("Yees! I found: " + nodeName + "at root position!" );
		Woman woman = (Woman) ((Node) man.getMarriedWith()).getData();
		if(woman.isEquals(nodeName));
			System.out.println("Yees! I found: " + nodeName + "at Woman root position!" );
		searchChild(root, nodeName);
	}
	
	private void searchChild(Node actual, String nodeName) {				//that works?
		Man man = (Man) actual.getData();
		Woman woman = (Woman) ((Node) man.getMarriedWith()).getData();
		if(man.isEquals(nodeName)) System.out.println("Found");
		if(woman.isEquals(nodeName)) System.out.println("Found");
		
		for(Node filho : actual.getChildNodes()) {  
			  System.out.println();  				//probably not
		}  
	}
	*/

	/*
	public void insertWoman(String parentName, Node nodeToInsert){			//this is useful?
		//needs to implement
		Node parent = searchNode(parentName);
		parent.addChild(nodeToInsert);
	}
	
	public void insertMan(String parentName, Node nodeToInsert){
		//needs to implement
		Node parent = searchNode(parentName);
		parent.addChild(nodeToInsert);
	}
	*/

	
}
